﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sm4
{
	class Program
	{
		static void Main(string[] args)
		{
			// Nuget 安装 BouncyCastle.Crypto 1.9.0 版本, .net formwork最低版本是4.0
			//明文手机号码
			string plainText = "{ \"phone\": \"你的手机号码\"}";
			 plainText = " { phone: \"你的手机号码\",captcha: \"收到的验证码\"}";
			Sm4Crypto sm4Crypto = new Sm4Crypto();
			//密钥转十六进制
			sm4Crypto.hexString = true;
			sm4Crypto.secretKey = "你的密钥";
			sm4Crypto.secretKey = StringToHexString(sm4Crypto.secretKey);
			//对获取验证码的手机号码进行加密, 然后用这个加密串就可以请求了
			string CryptoStr = sm4Crypto.Encrypt_ECB_Str(plainText);


			//解密; 
			//把获取的data转成bytes, 然后再把bytes转成字符串
			//  byte[] bytes = Convert.FromBase64String("24yu0M1dVnoBUP+7clJhaADhjuONAr2SWOBCnja8EsdBZSek06ZXRO1QAoEwGWUM/rPtL83YrAqbJ1P5/7pS43oWzNwNndMs9m67THwdRkYopV1CxIchj2Yb7uHDpxAiXT2kdYouXHSezJsKPMl9VA==");
			//  string temp = BitConverter.ToString(bytes).Replace("-", "").ToLower();

			// 解密;
			// 示例串
 //           {
 //               "msg": "请求成功",
	//"other": "this u code",
	//"code": 200,
	//"data": "5d14c1ed55baef318e33518c1feeba0920ae8b2da1f8e8b7f24cf7b1518f36d22aeb36122a0b0aaf991290cdd485bdf3a2577c9aa4b1a5300e767d7449af35d8793c660dc2bac71e611fbe524ad1586c1356f1e75b483b459d9b206ccb6d21a30ae8940abc0650006a9f8fbde94f9ad21fea20d05529f83a85e738c69971a16b",
	//"timestamp": 1659090205457
 //}
			string temp = "5d14c1ed55baef318e33518c1feeba0920ae8b2da1f8e8b7f24cf7b1518f36d22aeb36122a0b0aaf991290cdd485bdf3a2577c9aa4b1a5300e767d7449af35d8793c660dc2bac71e611fbe524ad1586c1356f1e75b483b459d9b206ccb6d21a30ae8940abc0650006a9f8fbde94f9ad21fea20d05529f83a85e738c69971a16b";

			string plainText_temp = sm4Crypto.Decrypt_ECB_Str(temp);

			Console.WriteLine(plainText_temp);

			Console.ReadLine();



			//如果你的.net Fromwork版本4.5或更高, 可以使用下面的代码, 需要nuget引入KYSharp.SM 2.0
			//SM4Utils sm4 = new SM4Utils();
			//sm4.secretKey = "密钥";
			//sm4.hexString = false;

			//System.Console.WriteLine("ECB模式");
			//string cipherText = sm4.Encrypt_ECB(plainText);
			//System.Console.WriteLine("密文: " + cipherText);
			//System.Console.WriteLine("");


			////对接口返回来的内容进行解密
			//string tempCipherText = "4iapykFkq2+u8Z96m32elp39kJOxEnRznZS/YeDncxsJGeWquWEI7QNwHVndcXzy";

			//// tempCipherText = Base64Decode(tempCipherText);
			//tempCipherText = StringToHexString(tempCipherText);
			//sm4.hexString = true;

			//string tempPlainText = sm4.Decrypt_ECB(tempCipherText);
			//System.Console.WriteLine("明文: " + plainText);
			//System.Console.WriteLine("");

			//System.Console.WriteLine("CBC模式");
			//sm4.iv = "您的密钥";
			//cipherText = sm4.Encrypt_CBC(plainText);
			//System.Console.WriteLine("密文: " + cipherText);
			//System.Console.WriteLine("");

			//plainText = sm4.Decrypt_CBC(cipherText);
			//System.Console.WriteLine("明文: " + plainText);

			//Console.ReadLine();


		}

		/// <summary>
		/// 字符串转16进制字符
		/// </summary>
		/// <param name="_str">字符串</param>
		/// <param name="encode">编码格式</param>
		/// <returns></returns>
		private static string StringToHexString(string str )
		{
			Encoding encode = Encoding.UTF8;
			//去掉空格
			str = str.Replace(" ", "");
			//将字符串转换成字节数组。
			byte[] buffer = encode.GetBytes(str);
			//定义一个string类型的变量，用于存储转换后的值。
			string result = string.Empty;
			for (int i = 0; i < buffer.Length; i++)
			{
				//将每一个字节数组转换成16进制的字符串，以空格相隔开。
				result += Convert.ToString(buffer[i], 16);
			}
			return result;
		} 
		
	}


  
}
